import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'valuationAnalysisM',
  state: {
    productList: [],
  },
  effects: {
    // 估值解析产品
    *valuationCheckProductInfo({ payload }, { call, put }) {
      const res = yield call(api.valuationAnalysis.valuationCheckProductInfo, payload);
      if (res && res.code == 0) {
        let tempArr = res.data?.dataSource || [];
        tempArr.forEach((item) => {
          item.title = `${item.code}-${item.name}`;
          item.value = item.code;
        });
        yield put({
          type: 'save',
          payload: {
            productList: tempArr,
          },
        });
      }
    },
    // 估值解析列表
    *getTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.getAnalysisTable, payload);
      let columns = [
        { key: 'dimension', title: '解析维度', width: 90 },
        { key: 'code', title: '代码', width: 180 },
        { key: 'name', title: '名称', width: 180 },
        { key: 'assetType', title: '资产类型', width: 180 },
        { key: 'applicantTime', title: '最新解析成功时间', width: 180 },
        { key: 'endDate', title: '到期日', width: 120 },
      ];
      if (res && res.code == 0) {
        callback('success', {
          dataSource: res.data.dataSource,
          pageInfo: res.data.pageInfo,
          columns,
        });
      } else {
        callback('fail');
        message.error(res?.msg || '查询估值解析列表失败！');
      }
    },

    // 估值解析记录
    *getAnalysisRecord({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.getAnalysisRecord, payload);
      let columns = [
        { key: 'applyDate', title: '申请时间', width: 180 },
        { key: 'applicant', title: '申请人', width: 150 },
        { key: 'fileName', title: '文件名' },
        { key: 'status', title: '申请状态' },
      ];
      if (res && res.code == 0) {
        callback('success', {
          dataSource: res.data.dataSource,
          pageInfo: res.data.pageInfo,
          columns,
        });
      } else {
        callback('fail');
        message.error(res?.msg || '查询估值解析记录失败！');
      }
    },

    // 新增估值解析
    *addAnalysis({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.addAnalysis, payload);
      if (res && res.code == 0) {
        callback('success', res.data);
        message.success('操作成功');
      } else {
        callback('fail');
        message.error(res?.msg || '新增估值解析失败！');
      }
    },

    //获取估值时间
    *getTime({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.getTime, payload);
      if (res && res.code == 0) {
        callback('success', res.data);
      } else {
        callback('fail');
      }
    },

    // 获取估值解析列表
    *findanalysis({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.findanalysis, payload);
      if (res && res.code == 0) {
        callback('success', res);
      } else {
        callback('fail');
      }
    },

    // 保存估值解析
    *saveValuateAnalysis({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.saveValuateAnalysis, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success('保存成功');
      } else {
        callback('fail');
        message.error(res?.message || '保存失败！');
      }
    },

    // 查批量申请异常列表
    *getAbnormalData({ payload, callback }, { call, put }) {
      const res = yield call(api.valuationAnalysis.getAbnormalData, payload);
      if (res && res.code == 0) {
        callback('success', {
          dataSource: res.data.dataSource,
        });
      } else {
        callback('fail');
        message.error(res?.msg || '查询失败！');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
